<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row :gutter="20">
      <el-col :span="14">
      <el-input v-model="listQuery.title" placeholder="机器ID/机器名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>

      </el-col>
      <el-col :span="10" style="text-align:right;">
      <el-button class="filter-item" type="primary" @click="handleCreate">
        新建
      </el-button>
      <el-button class="filter-item"  @click="handleDisabled()">
        禁用
      </el-button>
      <el-button class="filter-item"  @click="handleAllowed()">
        启用
      </el-button>
      <el-button class="filter-item"  @click="handleDeleteBatch()">
        删除
      </el-button>

      </el-col>
    </el-row>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="名称" prop="id" sortable="custom" align="center" width="140" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
          
        </template>
      </el-table-column>
      <el-table-column label="识别码" prop="id" sortable="custom" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.code }}</span>
          <i @click="handleCopy(row)" class="el-icon-document-copy"></i>
        </template>
      </el-table-column>
      <el-table-column label="主机" prop="id" sortable="custom" align="center" width="140" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.clientIP }}</span>
          
        </template>
      </el-table-column>
      <el-table-column label="合约" width="300">
        <template slot-scope="{row}">
          <el-tag v-for="(symbolData,index) in row.setting" :key="index" style="margin-right:20px; margin-bottom:10px;">{{symbolData.symbol}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最后更新" width="200">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.updatedAt }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status.value == 6" type="waring" size="mini" @click="handleModifyStatus(row,5)">
            禁用
          </el-button>
          <el-button v-if="row.status.value != 6 " size="mini" @click="handleModifyStatus(row,6)">
            启用
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value == 5" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.start" :limit.sync="listQuery.perpage" @pagination="getList" />

  </div>
</template>

<script>
import { getBroilerList,changeBroilerStatusByID,removeBroilerByID  } from '@/api/foundation/broiler'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'knowledgeAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      selectedIDs:[],
      listQuery: {
        start: 1,
        perpage: 10,
        title: undefined,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getBroilerList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleSelectionChange(item) {
      this.selectedIDs = []
      item.forEach((row)=>{
        this.selectedIDs.push(row['identity'])
      });
    },
    handleCopy:function(item){
      
    },
    handleShow:function(item){
      this.$router.push({ path: 'broiler-detail', query: { 'broilerID': item.ID }})
    },
    handleCreate:function(){
      this.$router.push({ path: 'broiler-add'})
    },
    handleUpdate:function(item){
      this.$router.push({ path: 'broiler-add', query: { 'broilerID': item.ID }})
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      changeBroilerStatusByID({
        "broilerID":row.ID,
        "status":status
      }).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleDelete(row) {
      removeBroilerByID({
        "broilerID":row.ID
      }).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
